﻿@using ZBJF.Warranty.BLL
@using ZBJF.Warranty.Common.Enums.InsuranceWarranty
@using ZBJF.Warranty.WebProvider
@using EnumHelper = ZBJF.Warranty.Common.Common.EnumHelper
@using ZBJF.Warranty.Common.Enums.InfoCollect
@using ZBJF.Warranty.Model
@model ZBJF.Warranty.Model.InsuranceWarrantyRecord
@{
    ViewBag.Title = "维修中工单详情";
}
@Html.HiddenFor(m => m.ID)
@Html.HiddenFor(m => m.InsuranceOrderId)
<!--维修中心审核-->
@RenderPage("~/Views/WorkOrderCommonInfo/AuditInfo.cshtml", new { AuditProjectType = (byte)AuditProjectType.RepairCenter })
<h4>维修基本信息</h4>
<form id="baseRepairInfo">
    <table class="table table-responsive table-bordered table-hover">
        <tr>
            <th>
                接机时间
            </th>
            <td>
                @Html.TextBoxFor(m => m.ServiceProviderReceiveMachineTime, new { @class = "Wdate", onclick = "WdatePicker({ dateFmt: 'yyyy-MM-dd HH:mm:ss' })" })
            </td>
            <th>随机附件</th>
            <td>
                @Html.TextBoxFor(m => m.WithTheMachineAccessories, new { @class = "w-p98" })
            </td>
            <th>维修工程师</th>
            <td>
                @Html.TextBoxFor(m => m.RepairEngineerUser, new { @class = "w-p98" })
            </td>
        </tr>
    </table>
</form>
<h4>维修具体项目</h4>
<form id="formInsuranceWarrantyRecordAccessories" name="formInsuranceWarrantyRecordAccessories">
    <input type="hidden" name="insuranceWarrantyRecordID" value="@Model.ID" />
    <table class="table table-striped table-bordered table-hover" id="tbInsuranceWarrantyRecordAccessories">
        <thead>
            <tr>
                <th>序号</th>
                <th>维修配件</th>
                <th>
                    配件品质
                </th>
                <th>配件价格</th>
            </tr>
        </thead>
        <tbody>
            @* ReSharper disable once InconsistentNaming *@
            @{
                var insuranceWarrantyRecordAccessories = (Model.InsuranceWarrantyRecordAccessories.Any() ? Model.InsuranceWarrantyRecordAccessories : new List<InsuranceWarrantyRecordAccessories>() { new InsuranceWarrantyRecordAccessories { } }).ToList();
                for (int i = 0; i < insuranceWarrantyRecordAccessories.Count; i++)
                {
                    <tr class="text-center" id="trInsuranceWarrantyRecordAccessories_@(i)">
                        <td>
                            @(i + 1)
                        </td>
                        <td>
                            @Html.HiddenFor(m => insuranceWarrantyRecordAccessories[i].ID)
                            @Html.HiddenFor(m => insuranceWarrantyRecordAccessories[i].InsuranceWarrantyRecordID)
                            @Html.DropDownListFor(m => insuranceWarrantyRecordAccessories[i].AccessoriesTypeID, ConvertToSelector.Instance.AccessoriesTypeConvertToSelectList(Model.InsuranceOrder.ProductTypeId ?? 0, addEmptySelect: false), new { validate = "{required:true,messages:{required:'请选择维修配件！'}}", onchange = "accessoriesTypeIDChange(this)" })
                        </td>
                        <td>
                            @Html.DropDownListFor(m => insuranceWarrantyRecordAccessories[i].AccessoriesLevelID, ConvertToSelector.Instance.AccessoriesLevelConvertToSelectList(selectedValue: insuranceWarrantyRecordAccessories[i].AccessoriesLevelID), new { validate = "{required:true,messages:{required:'请选择维修配件品质'}}" })
                        </td>
                        <td>
                            @Html.TextBoxFor(m => insuranceWarrantyRecordAccessories[i].AccessoriesPrice, new { validate = "{number:true,messages:{required:'请填写配件价格'}}", onkeyup = "onlyInputDecimal(this,2)", MaxLength = "9" })
                        </td>
                    </tr>
                }
            }
        </tbody>
    </table>
</form>
<!--维修完成收集信息-->
@RenderPage("~/Views/WorkOrderCommonInfo/InfoCollectItems.cshtml", new { CollectWithType = (byte)CollectWithType.CompleteCollect })

@RenderPage("~/Views/WorkOrderCommonInfo/AuditInfo.cshtml", new { AuditProjectType = (byte)AuditProjectType.MaintenanceCompletionTest })

@RenderPage("~/Views/WorkOrderCommonInfo/InsuranceOrderFeedbackHistory.cshtml")

<h4>寄回物流信息</h4>
@{
    var insuranceWarrantyRecordExpressInfo = Model.InsuranceWarrantyRecordExpressInfo.FirstOrDefault(o => o.ExpressSendByWhoType == (byte)ExpressSendByWhoType.RepairCenterSendToUser) ?? new InsuranceWarrantyRecordExpressInfo();
}
<form id="fromExpressInfo" enctype="multipart/form-data" method="post">
    <table class="table table-responsive table-bordered table-hover">
        @Html.HiddenFor(m => m.ID)
        <tr>
            <th>快递单号：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressNo, new { validate = "{required:true,messages:{required:'请填写快递单号'}}" })</td>
            <th>物流状态：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ExpressStatus)</td>
        </tr>
        <tr>
            <th>快递公司：</th>
            <td>@Html.DropDownListFor(n => insuranceWarrantyRecordExpressInfo.ExpressCompanyID, ZBJF.Warranty.WebProvider.ConvertToSelector.Instance.EnumTypeConvertToSelectList(typeof(ExpressEnums), (byte)ExpressEnums.SF))</td>
            <th class="w120">寄件人：</th>
            <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserName, new { validate = "{required:true,messages:{required:'请填写寄件人'}}" })</td>

        </tr>
        <tr>
            <th class="w120">寄件人电话：</th>
            <td class="w-p25">@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.SendUserPhone, new { validate = "{required:true,messages:{required:'请填写寄件人电话'}}" })</td>
            <th>收件人：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserName, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.ReceiverUserName) ? Model.Reporter : insuranceWarrantyRecordExpressInfo.ReceiverUserName, validate = "{required:true,messages:{required:'请填写收件人'}}" })</td>
        </tr>
        <tr>
            <th>收件人电话：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserPhone, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.ReceiverUserPhone) ? Model.ReporterPhone : insuranceWarrantyRecordExpressInfo.ReceiverUserPhone, validate = "{required:true,messages:{required:'请填写收件人电话'}}" })</td>
            <th>收件人地址：</th>
            <td>@Html.TextBoxFor(n => insuranceWarrantyRecordExpressInfo.ReceiverUserAddress, new { Value = string.IsNullOrEmpty(insuranceWarrantyRecordExpressInfo.ReceiverUserAddress) ? Model.AccidentAddress : insuranceWarrantyRecordExpressInfo.ReceiverUserAddress, validate = "{required:true,messages:{required:'请填写收件人地址'}}" })</td>
        </tr>
    </table>
</form>
@if (ServiceFactory.InsuranceWarrantyRecordStatusServiceInstance.GetParentStatusCode(Model.Status) == (byte)InsuranceWarrantyStatus.InMaintain)
{
    <div class="form-actions text-center">
        <input class="btn btn-primary Edit-R" type="button" onclick="Save(1)" value="保存信息">
        <input class="btn btn-primary Edit-R" type="button" onclick="Save(2)" value="完成维修">
        <input class="btn btn-primary Edit-R" type="button" value="提交问题" id="btnSendOrderAgain" onclick="showLayerOpen('提交问题', '/ServiceProviderWorkOrder/FeedbackIssue?InsuranceWarrantyRecordID=@Model.ID','500px','400px')">
    </div>
}
<script src="/Scripts/uploadAdapter/jquery.loadscript.js"></script>
<script src="/Scripts/uploadAdapter/jquery.uploadAdapter.js"></script>
<script src="/Scripts/WarrantyRecord/warrantyRecord.js"></script>
<script>
    $(function () {
        //增加行
        $("#btnAddNewRow").click(function () {
            var html = $(document.getElementById("tbInsuranceWarrantyRecordAccessories").rows[1]).html();
            var lastTrId = $("#tbInsuranceWarrantyRecordAccessories tbody tr:last-child").attr("id");
            var newId = parseInt(lastTrId.substring(lastTrId.indexOf('_') + 1));
            var regExpOrderItem = new RegExp("0", "g"); //第一行序号都是0
            html = html.replace(regExpOrderItem, newId + 1);

            html = "<tr id='trInsuranceWarrantyRecordAccessories_" + (newId + 1) + "'>" + html + "</tr>";
            $("#tbInsuranceWarrantyRecordAccessories tbody").append(html);
            $("#tbInsuranceWarrantyRecordAccessories tbody tr:last-child input").val("");
            $("#tbInsuranceWarrantyRecordAccessories tbody tr:last-child td:first").html(newId + 2);
        });
    })
    //删除行
    function removeInsuranceWarrantyRecordAccessories(trInsuranceWarrantyRecordAccessoriesId) {
        if (($("#tbInsuranceWarrantyRecordAccessories tbody tr").length == 1)) {
            layer.alert("最后一行不能删除，至少保留一行！");
            return;
        }
        $("#" + trInsuranceWarrantyRecordAccessoriesId).remove();
        var i = 0;
        $("#tbInsuranceWarrantyRecordAccessories tbody tr").each(function () {
            $(this).find("td:first").html(i + 1);
            $(this).find("td:last").html("<a class='Edit-R ' href=\"javascript:void(0)\" title='delete' onclick=\"removeInsuranceWarrantyRecordAccessories('trInsuranceWarrantyRecordAccessories_" + i + "')\"><i class='icon-remove'></i></a>");
            $(this).attr("id", "trInsuranceWarrantyRecordAccessories_" + i);
            $(this).find("input").each(function () {
                var regExpID = new RegExp("_\\d\+__", "g"); //替换ID
                var regExpName = new RegExp("\\[\\d\+\\]", "g"); //替换Name

                var id = $(this).attr("id");
                var name = $(this).attr("name");

                $(this).attr("id", id.replace(regExpID, "_" + i + "__"));
                $(this).attr("name", name.replace(regExpName, "[" + i + "]"));

                var clickEvent = $(this).attr("onclick");
                if (clickEvent && clickEvent != "")
                    $(this).attr("onclick", clickEvent.replace(regExpID, "_" + i + "__"));

                var onkeyupEvent = $(this).attr("onkeyup");
                if (onkeyupEvent && onkeyupEvent != "")
                    $(this).attr("onkeyup", onkeyupEvent.replace(regExpID, "_" + i + "__"));
            });
            i++;
        });
    }

    function accessoriesTypeIDChange(obj) {
        if (obj.value == "") {
            $("#" + $(obj).attr("id").replace("AccessoriesTypeID", "AccessoriesLevelID")).html("<option value='0'>请选择</option>");
        } else {
            $.ajax({
                url: "/Common/GetAccessoriesLevelList?AccessoriesTypeID=" + obj.value,
                success: function (data) {
                    var html = "<option value=''>请选择</option>";
                    if (data && data.length > 0) {
                        for (var i = 0; i < data.length; i++) {
                            html += "<option value='";
                            html += data[i].Value;
                            html += "'>" + data[i].Text;
                            html += "</option>";
                        }
                    }
                    $("#" + $(obj).attr("id").replace("AccessoriesTypeID", "AccessoriesLevelID")).html(html);
                }
            });
        }
    }
    //保存信息
    function Save(saveType) {
        var baseRepairInfo = $('#baseRepairInfo').serializeJson();//维修基本信息
        var fromExpressInfo = $('#fromExpressInfo').serializeJson();//快递信息
        var auditInfoForm = $('#auditInfoForm').serializeJson();//审核信息
        var formInsuranceWarrantyRecordAccessories = $('#formInsuranceWarrantyRecordAccessories').serializeJson();//维修详情
        var param = $.extend({}, baseRepairInfo, fromExpressInfo, auditInfoForm, formInsuranceWarrantyRecordAccessories);//多个表单参数拼接
        param.Status = "@Model.Status";

        if (saveType == 2) {
            if (!$('#baseRepairInfo').valid()) {
                layer.alert("请填写维修基本信息！");
                return;
            }
            //验证信息收集
            if (!$('#formInsuranceWarrantyRecordAccessories').valid()) {
                layer.alert("请完善维修具体项目！");
                return;
            }
            var infoCollectComplete = true;
            $(".InfoCollectCanUploadPicCount").each(function () {
                //需要上传的图片数大于已经上传的图片数则视为没有完成信息收集
                if ($(this).text() >
                    $("#" + $(this).attr("id").replace("InfoCollectCanUploadPicCount", "InfoCollectHadUploadPicCount"))
                    .text())
                    infoCollectComplete = false;
            });
            if (!infoCollectComplete) {
                layer.alert("请完成信息收集！");
                return;
            }
            //验证审核信息
            if (!$("#auditInfoForm").valid()) {
                layer.alert("请选择审核结果！");
                return;
            }
        }


        layer.load();
        $.ajax({
            url: "/ServiceProviderWorkOrder/SaveCompleteMaintainInsuranceWarrantyRecord?ID=@Model.ID" + "&saveType=" + saveType,
            type: "Post",
            data: param,
            success: function (data) {
                layer.alert(data.message, function () {
                    layer.closeAll();
                    if (saveType == 2) {
                        location.reload();
                    }
                });
            }
        });
    }
</script>